var PacMan = function(){

	this.snd = new Sound ( this );

	this.munchSnd = 0;

};

var p = PacMan.prototype = new MazeCharacter();

p.move = function () {

	var px = this._x;
	var py = this._y;

	var vx = this.vx;
	var vy = this.vy;

	var current_dir = this.current_dir;

	// get User Control //

	var usercode = this.engine.getKeyCode();

	// calculate direction key //

	var bit = usercode + ( current_dir << 4 );

	// switch opposite Direction //

	if ( bit == 18 || bit == 33 ) {

		vx = -vx; current_dir = usercode;

	} else if ( bit == 132 || bit == 82 ) {

		vy = -vy; current_dir = usercode;

	}

	// onSnap //

	if ( this._x % 8 == 0 && this._y % 8 == 0 ) {

		var rx = this.rx = px >> 3;
		var ry = this.ry = py >> 3;

		this.onSnap( rx , ry );

		if ( usercode & 1 && ~map[ ry ][ rx + 1 ] & 1 ) {

			vx = 2; vy = 0; current_dir = usercode;

		}

		if ( usercode & 2 && ~map[ ry ][ rx - 1 ] & 1 ) {

			vx = -2; vy = 0; current_dir = usercode;

		}

		if ( usercode & 4 && ~map[ ry + 1 ][ rx ] & 1 ) {

			vx = 0; vy = 2; current_dir = usercode;

		}

		if ( usercode & 8 && ~map[ ry - 1 ][ rx ] & 1 ) {

			vx = 0; vy = -2; current_dir = usercode;

		}

		if ( current_dir & 8 ) {

			if ( ~map[ ry - 1 ][ rx ] & 1 ) {

				py += vy;

			} else {

				current_dir -= 8; vy = 0;

			}

		}

		if ( current_dir & 4 ) {

			if ( ~map[ ry + 1 ][ rx ] & 1 ) {

				py += vy;

			} else {

				current_dir -= 4; vy = 0;

			}

		}

		if ( current_dir & 2 ) {

			if ( ~map[ ry ][ rx - 1 ] & 1 ) {

				px += vx;

			} else {

				current_dir -= 2; vx = 0;

			}

		}

		if ( current_dir & 1 ) {

			if ( ~map[ ry ][ rx + 1 ] & 1 ) {

				px += vx;

			} else {

				current_dir -= 1; vx = 0;

			}

		}

	} else {

		px += vx;
		py += vy;

	}

	this.updateAnimation();

	// outside screen ? //

	if ( px > 240 ) { px = -24 } else if ( px < -24 ) { px = 240 };

	this.current_dir = current_dir;

	this.vx = vx;
	this.vy = vy;

	this._x = px;
	this._y = py;

	// update screen after setInterval //

	updateAfterEvent();

}

p.onSnap = function ( rx , ry ) {

	var engine = this.engine;

	var bitflag = map[ ry ][ rx ];

	if ( bitflag & 4 ) {

		this.snd.stop();
		this.snd.attachSound ( "snd_munch" + this.munchSnd );
		this.snd.start();

		this.munchSnd = 1 - this.munchSnd;

		map[ ry ][ rx ] -= 4;

		engine.addScore( 10 );

		engine.mazeContext.dots[ ry ][ rx ].removeMovieClip();

		delete engine.mazeContext.dots[ ry ][ rx ];

	} else if ( bitflag & 8 ) {

		map[ ry ][ rx ] -= 8;

		engine.addScore( 25 );

		engine.switchToPelletMode();

		engine.mazeContext.dots[ ry ][ rx ].removeMovieClip();

		delete this.engine.mazeContext.dots[ ry ][ rx ];

	}

}

p.updateAnimation = function () {

	     if ( this.vx ==  2 ) { this.gotoAndStop ( "dir1" ); this.animation.nextFrame(); }
	else if ( this.vx == -2 ) { this.gotoAndStop ( "dir2" ); this.animation.nextFrame(); }
	else if ( this.vy ==  2 ) { this.gotoAndStop ( "dir4" ); this.animation.nextFrame(); }
	else if ( this.vy == -2 ) { this.gotoAndStop ( "dir8" ); this.animation.nextFrame(); }
	else this.animation.stop();

}